<?php
if (WAY <> 'ajax')
    echo '<div id="poll">';

if (!is_null($this->poll)) {
?>
    <h2><?= $this->poll->question ?></h2>
    <p class="poll_description"><?= $this->poll->description ?></p>
<?php
    if ($this->already) {
        $sum = 0;
        foreach ($this->answers as $v)
            $sum += $v->votes;
        foreach ($this->answers as $v) {
            if ($sum <> 0) {
                $width = round($v->votes / $sum * 100);
            } else {
                $width = 0;
            }
            echo '<div class="answer">' . $v->answer . '</div>';
            echo '<div class="result">' . $width . '% (' . $v->votes . ')</div>';
            echo '<div class="bar"><div style="width: ' . $width . '%;" class="foreground"></div></div>';
        }
        echo '<div class="total">' . $this->t->_('Total votes') . ': ' . $sum . '</div>';
    } else {
        echo '<form action="' . SITE_URL . '/ajax/sid/' . $this->section->id . '" method="post" id="pollForm" accept-charset="UTF-8">';
        foreach ($this->answers as $v) {
            echo '<div class="form-item"><input type="radio" id="poll-choice-' . $v->id . '" name="choice" value="' . $v->id . '" class="form-radio" /><label class="option" for="poll-choice-' . $v->id . '">' . $v->answer . '</label></div>';
        }
        echo '<div class="form-item-submit"><input type="submit" name="submit" value="' . $this->t->_('Vote') . '"  class="form-submit" /></div>';
        echo '</form>';
?>
        <script type="text/javascript">
            $(document).ready(function() {
                var options = {
                    target:        '#poll'
                };
                $('#pollForm').ajaxForm(options);
            });
        </script>

<?php } 
}
if (WAY <> 'ajax')
    echo '</div>';
?>
